<?php

namespace Xiaoc\Api;

use PhalApi\Api;
use Xiaoc\Model\Examples\CURD;

/**
 * Xiaoc首页
 *
 * @author: zhy <zhy302813@163.com> 2017-8-19
 */
class Home extends Api
{
    public function getRules()
    {

        return array(
            'goodslist' => array(
                'page' => array('name' => 'page', 'require' => true, 'default' => 1, 'desc' => '页数')
            )
        );

    }

    /**
     * 首页
     * @desc 首页幻灯片,导航,特价商品,四块什么鬼,海外特惠
     * @return int ad_id ID
     * @return string ad_name 名称
     * @return string ad_link 链接
     * @return string ad_code (host+)图片路径
     */
    public function index()
    {
        //首页幻灯片
        $banner = \PhalApi\DI()
            ->notorm
            ->touch_ad
            ->select('ad_id, ad_name, ad_link,ad_code')
            ->where(array('position_id' => 259, 'start_time < ?' => gmtime(), 'end_time > ?' => gmtime()))
            ->order('ad_id desc')
            ->limit(0, 6);
        //首页导航
        $nav = \PhalApi\DI()
            ->notorm
            ->touch_nav
            ->select('id,name,apiurl,apipic')
            ->where(array('ifshow' => 1));
        //特价商品
        $promote = \PhalApi\DI()
            ->notorm
            ->goods
            ->select('goods_id,shop_price,promote_price,goods_thumb')
            ->where(array('is_promote' => 1))
            ->order('last_update desc')
            ->limit(0, 3);
        //首页中部广告位
        $ad = \PhalApi\DI()
            ->notorm
            ->touch_ad
            ->select('ad_id, ad_name, ad_link,ad_code')
            ->where(array('position_id' => 260, 'start_time < ?' => gmtime(), 'end_time > ?' => gmtime()))
            ->order('ad_id desc')
            ->limit(0, 4);
        //海外特惠
        $model = new \Xiaoc\Model\Goods();
        $goods = $model->importGoods();
        return array('banner' => $banner, 'nav' => $nav, 'promote' => ['url' => '', 'lists' => $promote], 'ad' => $ad, 'goods' => $goods);
    }

    /**
     * 校园汇活动
     * @desc 首页校园汇活动
     * @return int goods_id ID
     * @return string goods_name 名称
     * @return string shop_price 价格
     * @return string market_price 市场价
     * @return string goods_thumb (host+)图片路径
     */
    /*public function activity()
    {
        $actInfo = \PhalApi\DI()
            ->notorm
            ->favourable_activity
            ->select('act_id, start_time, end_time,act_range_ext,act_type_ext,act_type')
            ->where(array('review_status' => 3, 'act_range' => 3, 'start_time < ?' => gmtime(), 'end_time > ?' => gmtime()))
            ->order('act_id desc')
            ->fetchRow();
        $goods_id = explode(',', $actInfo['act_range_ext']);
        $goods = \PhalApi\DI()
            ->notorm
            ->goods
            ->select('goods_name,goods_id,goods_thumb,shop_price,market_price')
            ->where('goods_id', $goods_id)
            ->limit(0, 6);
        return array('list' => $goods, 'time' => $actInfo['end_time'] - gmtime(), 'time2' => $actInfo['end_time'] + 8 * 3600);
    }*/

    /**
     * 首页广告位
     * @desc 校园汇活动下广告
     * @return int ad_id ID
     * @return string ad_name 名称
     * @return string ad_link 链接
     * @return string ad_code (host+)图片路径
     */
    /*public function adver()
    {
        $adver = \PhalApi\DI()
            ->notorm
            ->touch_ad
            ->select('ad_id, ad_name, ad_link,ad_code')
            ->where(array('position_id' => 1005, 'start_time < ?' => gmtime(), 'end_time > ?' => gmtime()))
            ->order('ad_id desc')
            ->limit(0, 6);
        return $adver;
    }*/

    /**
     * 众筹模块
     * @desc 众筹列表、广告
     * @return int ad_id ID
     * @return string ad_name 名称
     * @return string ad_link 链接
     * @return string ad_code (host+)图片路径
     * @return int act_id 活动ID
     * @return string goods_name 商品名称
     * @return string act_name 活动名称
     * @return int amount 达到数量
     * @return float price 享受价格
     * @return float deposit 保证金
     * @return int gift_integral 赠送积分数
     * @return int restrict_amount 限购数量
     * @return int join_number 参与数量
     */
    /*public function crowdlist()
    {
        //众筹广告
        $data["adver"] = \PhalApi\DI()
            ->notorm
            ->touch_ad
            ->select('ad_id, ad_name, ad_link,ad_code')
            ->where(array('position_id' => 1006, 'start_time < ?' => gmtime(), 'end_time > ?' => gmtime()))
            ->order('ad_id desc')
            ->fetchRow();
        //众筹列表
        $data["list"] = \PhalApi\DI()
            ->notorm
            ->goods_activity
            ->select('act_id, goods_name,activity_thumb, act_name,goods_id,ext_info,start_time,end_time')
            ->where(array('review_status' => 3, 'start_time < ?' => gmtime(), 'end_time > ?' => gmtime(), 'act_type' => 1))
            ->limit(0, 4)
            ->order('act_id desc')
            ->fetchAll();
        //获取表前缀
        $prefix = \PhalApi\DI()->config->get('dbs.tables.__default__.prefix');
        foreach ($data["list"] as $key => $vo) {
            $sql = "select SUM(og.goods_number) as goods_number from "
                . $prefix . "order_info as oi left join "
                . $prefix . "order_goods as og on oi.order_id = og.order_id "
                . "where og.goods_id = " . $vo['goods_id'] . "  and oi.extension_code ='group_buy' and oi.add_time > " . $vo['start_time'] . " and oi.add_time < " . $vo['end_time']
                . " group by og.goods_id";
            $rows = \PhalApi\DI()->notorm->order_info->queryAll($sql, array());
            $data["list"][$key]["ext_info"] = unserialize($vo["ext_info"]);
            $data["list"][$key]['join_number'] = isset($rows[0]['goods_number']) ? $rows[0]['goods_number'] : 0;
        }
        return $data;
    }*/

    /**
     * 试用模块
     * @desc 试用模块、广告
     * @return int ad_id ID
     * @return string ad_name 名称
     * @return string ad_link 链接
     * @return string ad_code (host+)图片路径
     * @return int act_id 活动ID
     * @return string goods_name 商品名称
     * @return string act_name 活动名称
     * @return int total_num 总申请量
     * @return int gift_integral 赠送积分
     * @return int gift_num 赠送份数
     */
    /*public function trylist()
    {
        //试用广告
        $data["adver"] = \PhalApi\DI()
            ->notorm
            ->touch_ad
            ->select('ad_id, ad_name, ad_link,ad_code')
            ->where(array('position_id' => 1006, 'start_time < ?' => gmtime(), 'end_time > ?' => gmtime()))
            ->order('ad_id desc')
            ->fetchRow();
        //试用列表
        $data["list"] = \PhalApi\DI()
            ->notorm
            ->goods_activity
            ->select('goods_id,act_id, goods_name, activity_thumb,act_name,goods_id,ext_info,start_time,end_time')
            ->where(array('review_status' => 3, 'start_time < ?' => gmtime(), 'end_time > ?' => gmtime(), 'act_type' => 7))
            ->limit(0, 4)
            ->order('act_id desc')
            ->fetchAll();
        foreach ($data["list"] as $key => $vo) {
            $data["list"][$key]["ext_info"] = unserialize($vo["ext_info"]);
            $goods = \PhalApi\DI()
                ->notorm
                ->goods
                ->select('shop_price')
                ->where('goods_id', $vo['goods_id'])
                ->fetchOne();
            $data["list"][$key]["shop_price"] = $goods['shop_price'] ? $goods['shop_price'] : 0;
        }
        return $data;
    }*/

    /**
     * 首页为您推荐
     * @desc 普通商品列表
     * @param int page 页数
     * @return int goods_id ID
     * @return string goods_name 名称
     * @return float shop_price 价格
     * @return string goods_thumb (host+)图片路径
     * @return float market_price 市场价
     * @return string goods_brief 商品简介（副标题）
     * @return string goods_product_tag 商品标签（多个以,分割）
     */
    public function goodslist()
    {
        $limit = ($this->page - 1) * 8;
        $list = \PhalApi\DI()
            ->notorm
            ->goods
            ->select('goods_id,goods_name,shop_price,market_price,goods_brief,goods_thumb,goods_product_tag')
            ->where(array('review_status > ?' => 2, 'goods_number > ?' => 0, 'is_on_sale' => 1))
            ->order('goods_id desc')
            ->limit($limit, 8);
        return $list;
    }

    /**
     * 试用模块
     * @desc 试用模块、广告
     * @return int ad_id ID
     * @return string ad_name 名称
     * @return string ad_link 链接
     * @return string ad_code (host+)图片路径
     * @return int act_id 活动ID
     * @return string goods_name 商品名称
     * @return string act_name 活动名称
     * @return int total_num 总申请量
     * @return int gift_integral 赠送积分
     * @return int gift_num 赠送份数
     */
    public function trylist()
    {
        //试用广告
        $data["adver"] = \PhalApi\DI()
            ->notorm
            ->touch_ad
            ->select('ad_id, ad_name, ad_link,ad_code')
            ->where(array('position_id' => 1006, 'start_time < ?' => gmtime(), 'end_time > ?' => gmtime()))
            ->order('ad_id desc')
            ->fetchRow();
        //试用列表
        $data["list"] = \PhalApi\DI()
            ->notorm
            ->goods_activity
            ->select('goods_id,act_id, goods_name, activity_thumb,act_name,goods_id,ext_info,start_time,end_time')
            ->where(array('review_status' => 3, 'start_time < ?' => gmtime(), 'end_time > ?' => gmtime(), 'act_type' => 7))
            ->limit(0, 4)
            ->order('act_id desc')
            ->fetchAll();
        foreach ($data["list"] as $key => $vo) {
            $data["list"][$key]["ext_info"] = unserialize($vo["ext_info"]);
            $goods = \PhalApi\DI()
                ->notorm
                ->goods
                ->select('shop_price')
                ->where('goods_id', $vo['goods_id'])
                ->fetchOne();
            $data["list"][$key]["shop_price"] = $goods['shop_price'] ? $goods['shop_price'] : 0;
        }
        return $data;
    }

}
